package model;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 
 * @author Michael Elgart, tyy
 *
 */
public class CSVparser extends FileParser {
    private static final String COMMA = ",";
    @Override
    public void readInput (InputStream stream, DataTable table) {
        //if the file has a first line, process this line into column names
        Scanner input = new Scanner(stream);
        if (input.hasNextLine()) {
            String[] columnNames = input.nextLine().split(COMMA);
            List<ArrayList<String>> contentStrings = initialize2DArrayList(columnNames);
            while (input.hasNextLine()) {
                int i = 0;
                for (String data : input.nextLine().split(COMMA)) {
                    try {
                        contentStrings.get(i).add(data);
                    }
                    catch (IndexOutOfBoundsException e) {
                        throw new ModelException("Incorrectly formatted data");
                    }
                    i++;
                }
            }
            convertColumns(table, columnNames, contentStrings);
        }
    }

}
